package com.example.intenttrial;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SDCardActivity extends Activity {

	Button btn;
	Button btnCreate;
	Button btnCreateImg;
	Button btnReadImg;
	TextView tv;
	EditText edtxt;
	
	String dirrectory;
	
	static final String PATH = "/sdcard/temp/";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.screen_sdcard);

		mapUI();

	}

	private void mapUI() {
		btnCreate = (Button) findViewById(R.id.btn_create);
		btn = (Button) findViewById(R.id.button1);
		btnCreateImg = (Button) findViewById(R.id.btn_writeimg);
		btnReadImg = (Button) findViewById(R.id.btn_readimg);
		tv = (TextView) findViewById(R.id.textView1);
		edtxt = (EditText) findViewById(R.id.editText1);

		btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				tv.setText(getFolders());
			}
		});

		btnCreate.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				addDirectory();
			}
		});

		btnCreateImg.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				writeImage();
			}
		});

		btnReadImg.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				readImage();
			}
		});
		
	}

	private String getFolders() {
		String folders = "";

		File sdCardRoot = new File(PATH);

		String[] list = sdCardRoot.list();
		for (String s : list) {
			folders += s + "\n";
		}

		return folders;
	}

	private void addDirectory() {
//		dirrectory = edtxt.getText().toString();
//		
//		File sdCardRoot = new File("/sdcard/" + dirrectory + "/");
//
//		sdCardRoot.mkdir();
//
//		System.out.println(getFolders());
	}

	private void writeImage() {

		Bitmap bmp = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
		bmp.setPixel(99, 99, Color.CYAN);
		String name = edtxt.getText().toString();

		File dir = new File(PATH);
		if (!dir.exists())
			dir.mkdirs();
		
		File file = new File(dir, name);
		
		FileOutputStream out = null;
		
		try {
			out = new FileOutputStream(file);
			bmp.compress(Bitmap.CompressFormat.PNG, 85, out);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	private void readImage() {
		
		String name = edtxt.getText().toString();
		
		Bitmap bmp = BitmapFactory.decodeFile(PATH + name);
		
		printBitMap(bmp);
	}

	private void printBitMap(Bitmap bmp) {
		int height = bmp.getHeight();
		int width = bmp.getWidth();
		
		int[] pixels = new int[height * width];
		
		bmp.getPixels(pixels, 0, width, 0, 0, width, height);
		
		for (int i = 0; i < pixels.length; i++) {
			
			int currPixel = pixels[i];
			
			System.out.println("R: " + Color.red(currPixel) 
					+ " G: " + Color.green(currPixel) 
					+ " B: " + Color.blue(currPixel));
		}
		
		
	}
	
}
